<template>
  <div class="app-container">

    <u-page title="二维火传输">

      <u-filtered>
        <el-form :inline="true" :model="listQuery" class="search-form">
          <el-form-item label="餐厅" prop="sh">
            <el-select v-model="listQuery.sh" filterable placeholder="选择餐厅" clearable style="width: 180px" class="select">
              <el-option
                v-for="item in restaurant"
                :key="item.ewh_deptid"
                :label="item.ewh_dept_name"
                :value="item.ewh_deptid"
              />
            </el-select>
          </el-form-item>
          <el-form-item label="二维火订单号" prop="uuid">
            <el-input v-model="listQuery.uuid" placeholder="二维火订单号" clearable style="width: 200px" />
          </el-form-item>
          <el-form-item label="商友订单号" prop="jlbh">
            <el-input v-model="listQuery.jlbh" placeholder="商友订单号" clearable style="width: 150px" />
          </el-form-item>
          <el-form-item label="账单号" prop="ddh">
            <el-input v-model="listQuery.ddh" placeholder="账单号" clearable style="width: 120px" />
          </el-form-item>
          <el-form-item label="收银台" prop="sktno">
            <el-input v-model="listQuery.sktno" placeholder="收银台" clearable style="width: 100px" />
          </el-form-item>
          <el-form-item label="传输日期" prop="transferDateRange">
            <u-date-time-picker v-model="transferDateRange" value-format="yyyy-MM-dd HH:mm:ss" />
          </el-form-item>
          <el-form-item label="记录条数" prop="num">
            <el-input v-model="listQuery.num" placeholder="默认10条" clearable style="width: 100px" />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" @click="handleFilter">搜索</el-button>
          </el-form-item>
        </el-form>
      </u-filtered>

      <u-table
        :data="list"
        :loading="listLoading"
        :options="listOptions"
        :columns="columns"
        :page="false"
      >
        <template v-slot:right>
          <el-table-column label="操作" align="center" fixed="right">
            <template slot-scope="{ row }">
              <el-button type="text" @click="hdlEdit(row)">详情</el-button>
            </template>
          </el-table-column>
        </template>
      </u-table>

    </u-page>

    <u-dialog :title="textMap[dialogStatus]" :show.sync="dialogFormVisible" :footer="false">
      <el-form ref="dialogForm" :rules="rules" :model="dataForm" label-width="60px">

        <el-descriptions :column="1" border>
          <el-descriptions-item label="ID编号">{{ dataForm.id }}</el-descriptions-item>
          <el-descriptions-item label="二维火订单号">{{ dataForm.uuid }}</el-descriptions-item>
          <el-descriptions-item label="商友订单号" >{{ dataForm.jlbh }}</el-descriptions-item>
          <el-descriptions-item label="账单" >{{ dataForm.ddh }}</el-descriptions-item>
          <el-descriptions-item label="收银台">{{ dataForm.sktno }}</el-descriptions-item>
          <el-descriptions-item label="传输状态">
            <el-tag v-if="dataForm.status" type="success">成功</el-tag>
            <el-tag v-else type="danger">失败</el-tag>
          </el-descriptions-item>
          <el-descriptions-item label="传输时间">{{ dataForm.sj }}</el-descriptions-item>
          <el-descriptions-item label="备注">{{ dataForm.content }}</el-descriptions-item>
        </el-descriptions>

      </el-form>
    </u-dialog>

  </div>
</template>
<script>

export default {
  data() {
    return {
      api: this.$u.api.ShangyouTransfer,
      // 表格
      columns: [
        {
          prop: 'id',
          label: 'ID编号',
          width: '100'
        },
        {
          prop: 'SH',
          label: '餐厅',
          render: (h, params) => {
            const deptId = params.row.sh
            const { ewh_dept_name } = this.loadRestaurant(deptId)
            return h('el-tag', { props: { type: 'success' }}, ewh_dept_name)
          }
        },
        {
          prop: 'uuid',
          label: '二维火订单号',
          width: '350'
        },
        {
          prop: 'jlbh',
          label: '商友订单号'
        },
        {
          prop: 'ddh',
          label: '账单号'
        },
        {
          prop: 'sktno',
          label: '收银台',
          width: '80'
        },
        {
          prop: 'status',
          label: '传输状态',
          render: (h, params) => {
            if (params.row.status) {
              return h('el-tag', { props: { type: 'success' }}, '成功')
            } else {
              return h('el-tag', { props: { type: 'danger' }}, '失败')
            }
          }
        },
        {
          prop: 'sj',
          sortable: true,
          label: '传输时间'
        },
        {
          prop: 'content',
          label: '备注'
        }
      ],

      list: [],
      listLoading: false,
      listOptions: {
        mutiSelect: false, // boolean 是否多选
        stripe: true // boolean 斑马纹
      },
      listQuery: {
        sh: '',
        ddh: '',
        uuid: '',
        jlbh: '',
        sktno: '',
        num: '10'
      },

      dialogFormVisible: false,
      dialogStatus: '',
      textMap: {
        update: '查看详情',
        create: '新增传输'
      },
      dataForm: {},
      rules: {
        title: [
          { required: true, message: '请输入标题', trigger: ['blur'] }
        ]
      },
      // 删除选中数据
      selectData: [],
      newForm: {
        disabled: true
      },
      transferDateRange: [],
      restaurant: []
    }
  },
  created() {
    this.loadData()
  },
  methods: {
    // 初始化餐饮数据
    loadData() {
      this.api.restaurant().then(res => {
        this.restaurant = res.data
      })
    },
    // 获取餐厅
    loadRestaurant(deptId) {
      return this.restaurant.find(item => item.ewh_deptid === deptId)
    },
    // 日期格式转换
    dateFormat() {
      if (this.transferDateRange) {
        this.listQuery.beginDate = this.transferDateRange[0]
        this.listQuery.endDate = this.transferDateRange[1]
      } else {
        this.listQuery.beginDate = ''
        this.listQuery.endDate = ''
      }
    },
    // 搜索事件
    handleFilter() {
      this.dateFormat()
      this.hdlFilter()
    },
    // 获取列表数据
    hdlList() {
      const self = this
      // 打开加载状态
      self.listLoading = true
      // 查询列表数据
      self.api.twodfire(self.listQuery).then(res => {
        // 获取列表数据
        self.list = res.data
        // 关闭加载状态
        setTimeout(() => {
          self.listLoading = false
        }, 0.5 * 1000)
      })
    }
  }
}
</script>
<style lang="scss" scoped>
.select{
  ::v-deep{
    .el-input{
      width: 100% !important;
    }
  }
}
</style>

